package com.llkj.qingnianguoxue.mine;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

/**
 * 更换手机号
 */
public class ChangePhoneActivity extends BaseActivity implements View.OnClickListener {
    private static final int REQUEST_CHANGE_CODE = 8877;
    private EditText etPhone, etCode;//手机，验证码
    private TextView tvGetCode, btnNext;//获取验证码,下一步
    private TimeCount time;//倒计时
    private int durationTime = 60 * 1000;
    private int mFromClass;//2更换最后一步

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);
        setTitle("更换手机号", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);

        Intent intent = getIntent();
        mFromClass = intent.getIntExtra("from_class", 2);

        initView();
        initListener();

        time = new TimeCount(durationTime, 1000);
    }

    private void initView() {
        etPhone = (EditText) findViewById(R.id.et_phone);
        etCode = (EditText) findViewById(R.id.et_code);
        tvGetCode = (TextView) findViewById(R.id.tv_get_code);
        btnNext = (TextView) findViewById(R.id.tv_next);

        if (mFromClass == 1) {
            etPhone.setHint(getString(R.string.change_phone_new));
            btnNext.setText("绑定");
        }
    }

    private void initListener() {
        tvGetCode.setOnClickListener(this);
        btnNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String phone = etPhone.getText().toString().trim();
        switch (v.getId()) {
            case R.id.tv_get_code:
                if (!StringUtils.isPhoneNumber(phone)) {
                    ToastUtil.makeLongText(this, "请输入正确的手机号格式");
                    return;
                }
                time.start();
                loadWaitDialog();
                HttpMethod.loginFormcode(this, this, phone, null);
                break;
            case R.id.tv_next:
                String code = etCode.getText().toString();
                if (StringUtils.isEmpty(code)) {
                    ToastUtil.makeLongText(this, "请输入验证码");
                    return;
                }

                loadWaitDialog();
                HttpMethod.editMobile(this, this, phone, code, String.valueOf(mFromClass));
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_USER_EDIT_MOBILE) {
            if (mFromClass != 1) {
                Intent intent = new Intent(this, ChangePhoneActivity.class);
                intent.putExtra("from_class", 1);
                startActivityForResult(intent, REQUEST_CHANGE_CODE);
            } else {
                String phone = etPhone.getText().toString().trim();
                MyApplication.getInstance().sputil.put(ConstantUserInfo.MOBILE, phone);
                setResult(RESULT_OK);
                finish();
            }
        }else if(method == HttpMethod.HTTP_LOGIN_FORMCODE){//获取验证码
            ToastUtil.makeLongText(this,getString(R.string.send_code_success));
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
//        super.onhttpFailed(message, method);
        if (!StringUtils.isEmpty(message)) {
            ToastUtil.makeLongText(this, message);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            if (tvGetCode != null) {
                tvGetCode.setText("重新发送");
                tvGetCode.setClickable(true);
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            if (tvGetCode != null) {
                tvGetCode.setClickable(false);//防止重复点击
                tvGetCode.setText(millisUntilFinished / 1000 + "s");
            }
        }
    }
}
